import java.util.*;

/**
 * @author LKQ
 * @date 2022/2/23 22:39
 * @description
 */
public class Solution {
    public static void main(String[] args) {

    }
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        Map<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < arr2.length; i++) {
            map.put(arr2[i], i);
        }
        int n = arr1.length;
        List<int[]> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (int k : arr1) {
            if (map.containsKey(k)) {
                list1.add(new int[]{k, map.get(k)});
            } else {
                list2.add(k);
            }
        }
        Collections.sort(list1, Comparator.comparingInt(a -> a[1]));
        Collections.sort(list2);
        int[] ans = new int[n];
        int i;
        for (i = 0; i < list1.size(); i++) {
            ans[i] = list1.get(i)[0];
        }
        for (int j = 0; j < list2.size(); j++) {
            ans[i + j] = list2.get(j);
        }
        return ans;
    }
}
